//
//  JWKSearchBar.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/19.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit

class JWKSearchBar: UIView {

    @IBOutlet weak var searchTextField: UITextField!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    
    @IBOutlet weak var rightConstraint: NSLayoutConstraint!
    
    @IBAction func searchTextFieldBeginEditing(sender: AnyObject) {
        //只要处于编辑状态就让文本框的右侧间距=取消按钮的宽,显示取消--加上0.25s(系统默认动画时间就是0.25s)动画
        UIView.animateWithDuration(0.25) { 
            
            self.rightConstraint.constant = self.cancelBtn.bounds.width
            
            //一定要调用一下布局子控件,不然动画不显示
            self.layoutIfNeeded()
        }
        
        
    }

    @IBAction func cancelBtnClick(sender: AnyObject) {
        //点击取消就让文本框的右侧间距=0,隐藏取消
        UIView.animateWithDuration(0.25) {
            
            self.rightConstraint.constant = 0
            self.endEditing(true)
            //一定要调用一下布局子控件,不然动画不显示
            self.layoutIfNeeded()
        }
        
    }
    
    
    class func setupSearchBar() -> (JWKSearchBar){
    
        let searchBar = NSBundle.mainBundle().loadNibNamed("JWKSearchBar", owner: nil, options: nil).last as! JWKSearchBar
        
        
        return searchBar
    }
    
    override func awakeFromNib() {
//        super.awakeFromNib()
        
        let searchImageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        searchImageView.frame = CGRectMake(0, 0, 40, 40)
        
        //修改图片框的内容填充模式 center中心点
        searchImageView.contentMode = UIViewContentMode.Center
        
        searchTextField.leftView = searchImageView
        
        searchTextField.leftViewMode = UITextFieldViewMode.Always
    }

}
